
#include "connectionPool.hpp"
#include "friendmodel.hpp"

/**
 * @brief 插入好友关系（双向）
 *
 * @param userid
 * @param friendid
 */
void FriendModel::insert(int userid, int friendid)
{
    char sql1[1024] = {0};
    char sql2[1024] = {0};
    sprintf(sql1, "insert into friend values(%d,%d)", userid, friendid);
    sprintf(sql2, "insert into friend values(%d,%d)", friendid, userid);
    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    mysql->update(sql1);
    mysql->update(sql2);
}
/**
 * @brief 查询好友列表
 *
 * @param userid
 * @return vector<User> 好友信息集合
 */
vector<User> FriendModel::query(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "select id,name,state from user join friend on friendid=id where userid=%d", userid);

    shared_ptr<MySQL> mysql = ConnectionPool::getConnectionPool()->getMySQL();
    vector<User> ret;
    MYSQL_RES *res = mysql->query(sql);
    if (res != nullptr)
    {
        MYSQL_ROW row;
        while ((row = mysql_fetch_row(res)) != nullptr)
        {
            User user;
            user.setId(atoi(row[0]));
            user.setName(row[1]);
            user.setState(row[2]);
            ret.push_back(user);
        }
        mysql_free_result(res);
    }
    return ret;
}